home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / siggraphCD / lib / libtk / getset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  10.0 KB  |  319 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "tk.h"
  41. #include "private.h"
  42.  
  43. /******************************************************************************/
  44.  
  45. int tkGetColorMapSize(void)
  46. {
  47.  
  48.     if (!xDisplay) {
  49.     return 0;
  50.     } else {
  51.     return w.vInfoMain->colormap_size;
  52.     }
  53. }
  54.  
  55. /******************************************************************************/
  56.  
  57. void tkGetMouseLoc(int *x, int *y)
  58. {
  59.     int junk;
  60.  
  61.     *x = 0;
  62.     *y = 0;
  63.     XQueryPointer(xDisplay, w.wMain, (Window *)&junk, (Window *)&junk,
  64.           &junk, &junk, x, y, (unsigned int *)&junk);
  65. }
  66.  
  67. /******************************************************************************/
  68.  
  69. void tkGetSystem(GLenum type, void *ptr)
  70. {
  71.  
  72.     switch (type) {
  73.       case TK_X_DISPLAY:
  74.     ptr = (void *)xDisplay;
  75.     break;
  76.       case TK_X_WINDOW:
  77.     ptr = (void *)w.wMain;
  78.     break;
  79.     }
  80. }
  81.  
  82. /******************************************************************************/
  83.  
  84. void tkSetFogRamp(int density, int startIndex)
  85. {
  86.     XColor c[256];
  87.     int rShift, gShift, bShift, intensity, fogValues, colorValues;
  88.     int i, j, k;
  89.  
  90.     switch (w.vInfoMain->class) {
  91.       case DirectColor:
  92.     fogValues = 1 << density;
  93.     colorValues = 1 << startIndex;
  94.     for (i = 0; i < colorValues; i++) {
  95.         for (j = 0; j < fogValues; j++) {
  96.         k = i * fogValues + j;
  97.         intensity = i * fogValues + j * colorValues;
  98.         if (intensity > w.vInfoMain->colormap_size) {
  99.             intensity = w.vInfoMain->colormap_size;
  100.         }
  101.         intensity = (intensity << 8) | intensity;
  102.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  103.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  104.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  105.         c[k].pixel = ((k << rShift) & w.vInfoMain->red_mask) |
  106.                  ((k << gShift) & w.vInfoMain->green_mask) |
  107.                  ((k << bShift) & w.vInfoMain->blue_mask);
  108.         c[k].red = (unsigned short)intensity;
  109.         c[k].green = (unsigned short)intensity;
  110.         c[k].blue = (unsigned short)intensity;
  111.         c[k].flags = DoRed | DoGreen | DoBlue;
  112.         }
  113.     }
  114.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  115.     break;
  116.       case GrayScale:
  117.       case PseudoColor:
  118.     fogValues = 1 << density;
  119.     colorValues = 1 << startIndex;
  120.     for (i = 0; i < colorValues; i++) {
  121.         for (j = 0; j < fogValues; j++) {
  122.         k = i * fogValues + j;
  123.         intensity = i * fogValues + j * colorValues;
  124.         if (intensity > w.vInfoMain->colormap_size) {
  125.             intensity = w.vInfoMain->colormap_size;
  126.         }
  127.         intensity = (intensity << 8) | intensity;
  128.         c[k].pixel = k;
  129.         c[k].red = (unsigned short)intensity;
  130.         c[k].green = (unsigned short)intensity;
  131.         c[k].blue = (unsigned short)intensity;
  132.         c[k].flags = DoRed | DoGreen | DoBlue;
  133.         }
  134.     }
  135.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  136.     break;
  137.     }
  138.  
  139.     XSync(xDisplay, 0);
  140. }
  141.  
  142. /******************************************************************************/
  143.  
  144. void tkSetGreyRamp(void)
  145. {
  146.     XColor c[256];
  147.     float intensity;
  148.     int rShift, gShift, bShift, i;
  149.  
  150.     switch (w.vInfoMain->class) {
  151.       case DirectColor:
  152.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  153.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  154.             65535.0 + 0.5;
  155.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  156.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  157.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  158.         c[i].pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  159.              ((i << gShift) & w.vInfoMain->green_mask) |
  160.              ((i << bShift) & w.vInfoMain->blue_mask);
  161.         c[i].red = (unsigned short)intensity;
  162.         c[i].green = (unsigned short)intensity;
  163.         c[i].blue = (unsigned short)intensity;
  164.         c[i].flags = DoRed | DoGreen | DoBlue;
  165.     }
  166.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  167.     break;
  168.       case GrayScale:
  169.       case PseudoColor:
  170.     for (i = 0; i < w.vInfoMain->colormap_size; i++) {
  171.         intensity = (float)i / (float)w.vInfoMain->colormap_size *
  172.             65535.0 + 0.5;
  173.         c[i].pixel = i;
  174.         c[i].red = (unsigned short)intensity;
  175.         c[i].green = (unsigned short)intensity;
  176.         c[i].blue = (unsigned short)intensity;
  177.         c[i].flags = DoRed | DoGreen | DoBlue;
  178.     }
  179.     XStoreColors(xDisplay, w.cMapMain, c, w.vInfoMain->colormap_size);
  180.     break;
  181.     }
  182.  
  183.     XSync(xDisplay, 0);
  184. }
  185.  
  186. /******************************************************************************/
  187.  
  188. void tkSetOneColor(int index, float r, float g, float b)
  189. {
  190.     XColor c;
  191.     int rShift, gShift, bShift;
  192.  
  193.     switch (w.vInfoMain->class) {
  194.       case DirectColor:
  195.     rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  196.     gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  197.     bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  198.     c.pixel = ((index << rShift) & w.vInfoMain->red_mask) |
  199.           ((index << gShift) & w.vInfoMain->green_mask) |
  200.           ((index << bShift) & w.vInfoMain->blue_mask);
  201.     c.red = (unsigned short)(r * 65535.0 + 0.5);
  202.     c.green = (unsigned short)(g * 65535.0 + 0.5);
  203.     c.blue = (unsigned short)(b * 65535.0 + 0.5);
  204.     c.flags = DoRed | DoGreen | DoBlue;
  205.     XStoreColor(xDisplay, w.cMapMain, &c);
  206.     break;
  207.       case GrayScale:
  208.       case PseudoColor:
  209.     if (index < w.vInfoMain->colormap_size) {
  210.         c.pixel = index;
  211.         c.red = (unsigned short)(r * 65535.0 + 0.5);
  212.         c.green = (unsigned short)(g * 65535.0 + 0.5);
  213.         c.blue = (unsigned short)(b * 65535.0 + 0.5);
  214.         c.flags = DoRed | DoGreen | DoBlue;
  215.         XStoreColor(xDisplay, w.cMapMain, &c);
  216.     }
  217.     break;
  218.     }
  219.  
  220.     XSync(xDisplay, 0);
  221. }
  222.  
  223. /******************************************************************************/
  224.  
  225. void tkSetOverlayMap(int size, float *rgb)
  226. {
  227.     XColor c;
  228.     unsigned long *buf;
  229.     int max, i;
  230.  
  231.     if (w.vInfoOverlay->class == PseudoColor) {
  232.     max = (size > w.vInfoOverlay->colormap_size) ?
  233.           w.vInfoOverlay->colormap_size : size;
  234.     buf = (unsigned long *)calloc(max, sizeof(unsigned long));
  235.     XAllocColorCells(xDisplay, w.cMapOverlay, True, NULL, 0, buf, max-1);
  236.     for (i = 1; i < max; i++) {
  237.         c.pixel = i;
  238.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  239.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  240.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  241.         c.flags = DoRed | DoGreen | DoBlue;
  242.         XStoreColor(xDisplay, w.cMapOverlay, &c);
  243.     }
  244.     free(buf);
  245.     }
  246.  
  247.     XSync(xDisplay, 0);
  248. }
  249.  
  250. /******************************************************************************/
  251.  
  252. void tkSetRGBMap(int size, float *rgb)
  253. {
  254.     XColor c;
  255.     int rShift, gShift, bShift, max, i;
  256.  
  257.     switch (w.vInfoMain->class) {
  258.       case DirectColor:
  259.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  260.                           : size;
  261.     for (i = 0; i < max; i++) {
  262.         rShift = ffs((unsigned int)w.vInfoMain->red_mask) - 1;
  263.         gShift = ffs((unsigned int)w.vInfoMain->green_mask) - 1;
  264.         bShift = ffs((unsigned int)w.vInfoMain->blue_mask) - 1;
  265.         c.pixel = ((i << rShift) & w.vInfoMain->red_mask) |
  266.               ((i << gShift) & w.vInfoMain->green_mask) |
  267.               ((i << bShift) & w.vInfoMain->blue_mask);
  268.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  269.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  270.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  271.         c.flags = DoRed | DoGreen | DoBlue;
  272.         XStoreColor(xDisplay, w.cMapMain, &c);
  273.     }
  274.     break;
  275.       case GrayScale:
  276.       case PseudoColor:
  277.     max = (size > w.vInfoMain->colormap_size) ? w.vInfoMain->colormap_size
  278.                           : size;
  279.     for (i = 0; i < max; i++) {
  280.         c.pixel = i;
  281.         c.red = (unsigned short)(rgb[i] * 65535.0 + 0.5);
  282.         c.green = (unsigned short)(rgb[size+i] * 65535.0 + 0.5);
  283.         c.blue = (unsigned short)(rgb[size*2+i] * 65535.0 + 0.5);
  284.         c.flags = DoRed | DoGreen | DoBlue;
  285.         XStoreColor(xDisplay, w.cMapMain, &c);
  286.     }
  287.     break;
  288.     }
  289.  
  290.     XSync(xDisplay, 0);
  291. }
  292.  
  293. /******************************************************************************/
  294.  
  295. GLenum tkSetWindowLevel(GLenum level)
  296. {
  297.  
  298.     switch (level) {
  299.       case TK_OVERLAY:
  300.     if (TK_HAS_OVERLAY(w.type)) {
  301.         if (!glXMakeCurrent(xDisplay, w.wOverlay, w.cOverlay)) {
  302.         return GL_FALSE;
  303.         }
  304.     } else {
  305.         return GL_FALSE;
  306.     }
  307.     break;
  308.       case TK_RGB:
  309.       case TK_INDEX:
  310.     if (!glXMakeCurrent(xDisplay, w.wMain, w.cMain)) {
  311.         return GL_FALSE;
  312.     }
  313.     break;
  314.     }
  315.     return GL_TRUE;
  316. }
  317.  
  318. /******************************************************************************/
  319.